package com.iwords.service.persistence.impl;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.iwords.service.object.WordFrequencyDetailEntity;
import com.iwords.service.persistence.WordFreDetailPersistence;

/**
 * @author       BOLIN.HBC 
 * @time         2011-3-12    
 * @description  WORDFREDETAIL-PersistenceImpl        
 */
@Stateless
public class WordFreDetailPersistenceImpl implements WordFreDetailPersistence {
	@PersistenceContext(unitName = "iwords")
	private EntityManager em;

	protected EntityManager getEntityManager() {
		return em;
	}

	@SuppressWarnings("unchecked")
	public List<WordFrequencyDetailEntity> getWordFreDetailByWordId(String wordid) {
		Query query = getEntityManager().createQuery("select u from WordFrequencyDetailEntity u where u.wordid in (" + wordid + ")");
		return query.getResultList();
	}

	public List<WordFrequencyDetailEntity> getWordFreDetailListByIds(List<String> ids) {

		List<WordFrequencyDetailEntity> result = new ArrayList<WordFrequencyDetailEntity>();
		StringBuilder str = new StringBuilder();
		for (int i = 0; i < ids.size(); i++) {
			str.append(ids.get(i)).append(",");

			if ((i + 1) % 1000 == 0) {
				result.addAll(getWordFreDetailByWordId(str.substring(0, str.length() - 1)));
				str = new StringBuilder();
			}
		}
		if (str.length() > 0) {
			result.addAll(getWordFreDetailByWordId(str.substring(0, str.length() - 1)));

		}
		return result;
	}

}